home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6366 / 6366.xpi / components / xdGestureService.js < prev   
Text File  |  2009-11-17  |  4KB  |  166 lines

  1.  
  2. const Cc = Components.classes;
  3. const Ci = Components.interfaces;
  4.  
  5. const DB_FILE_NAME = "firegestures.sqlite";
  6. const BROWSER_ID  = "gesture_mappings";
  7. const BROWSER_URI = "chrome://firegestures/content/browser.rdf";
  8. const VIEWSOURCE_ID  = "viewsource_mapping";
  9. const VIEWSOURCE_URI = "chrome://firegestures/content/viewSource.rdf";
  10. const BUNDLE_URI = "chrome://firegestures/locale/firegestures.properties";
  11.  
  12. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. function xdGestureService() {
  20.     this._initService();
  21. }
  22.  
  23.  
  24. xdGestureService.prototype = {
  25.  
  26.  
  27.     classDescription: "Mouse Gesture Service",
  28.     contractID: "@xuldev.org/firegestures/service;1",
  29.     classID: Components.ID("{1d26f3e7-d92e-4bcc-ac79-9624bb181308}"),
  30.     QueryInterface: XPCOMUtils.generateQI([
  31.         Ci.nsISupports,
  32.         Ci.xdIGestureService
  33.     ]),
  34.  
  35.  
  36.  
  37.     _dbFile: null,
  38.  
  39.     _dbConn: null,
  40.  
  41.     _mappingsMeta: {},
  42.  
  43.     _namedMappings: {},
  44.  
  45.  
  46.  
  47.     _initService: function FGS__initService() {
  48.         if (this._dbFile)
  49.             return;
  50.         var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
  51.         this._dbFile = dirSvc.get("ProfD", Ci.nsILocalFile);
  52.         this._dbFile.append(DB_FILE_NAME);
  53.         this.registerMapping(BROWSER_ID, BROWSER_URI, this.getLocaleString("BROWSER"));
  54.         this.registerMapping(VIEWSOURCE_ID, VIEWSOURCE_URI, this.getLocaleString("VIEWSOURCE"));
  55.     },
  56.  
  57.     createHandler: function FGS_createHandler() {
  58.         var handler = Cc["@xuldev.org/firegestures/handler;1"].createInstance(Ci.xdIGestureHandler);
  59.         return handler;
  60.     },
  61.  
  62.     registerMapping: function FGS_registerMapping(aID, aURI, aName) {
  63.         if (aID in this._mappingsMeta)
  64.             return;
  65.         this._mappingsMeta[aID] = { uri: aURI, name: aName };
  66.     },
  67.  
  68.     getMapping: function FGS_getMapping(aID) {
  69.         if (aID in this._namedMappings)
  70.             return this._namedMappings[aID];
  71.         var meta = this._mappingsMeta[aID];
  72.         if (!meta)
  73.             throw Components.results.NS_ERROR_NOT_INITIALIZED;
  74.         var mapping = Cc["@xuldev.org/firegestures/mapping;1"].createInstance(Ci.xdIGestureMapping);
  75.         mapping.init(aID, meta.uri, meta.name);
  76.         this._namedMappings[aID] = mapping;
  77.         return mapping;
  78.     },
  79.  
  80.     getMappingForBrowser: function FGS_getMappingForBrowser() {
  81.         return this.getMapping(BROWSER_ID);
  82.     },
  83.  
  84.     getMappingsInfo: function FGS_getMappingsInfo() {
  85.         var ret = [];
  86.         for (var id in this._mappingsMeta) {
  87.             var meta = this._mappingsMeta[id];
  88.             ret.push({ id: id, uri: meta.uri, name: meta.name });
  89.         }
  90.         return ret;
  91.     },
  92.  
  93.     backupMappings: function FGS_backupMappings(aFile) {
  94.         if (!this._dbFile.exists())
  95.             throw Components.results.NS_ERROR_FAILURE;
  96.         if (aFile.exists())
  97.             aFile.remove(false);
  98.         this._dbFile.copyTo(aFile.parent, aFile.leafName);
  99.     },
  100.  
  101.     restoreMappings: function FGS_restoreMappings(aFile) {
  102.         if (aFile.equals(this._dbFile))
  103.             return;
  104.         if (this._dbConn) {
  105.             this._dbConn.close();
  106.             this._dbConn = null;
  107.         }
  108.         if (this._dbFile.exists())
  109.             this._dbFile.remove(false);
  110.         aFile.copyTo(this._dbFile.parent, DB_FILE_NAME);
  111.         this._dbFile = null;
  112.         this._initService();
  113.         for each (var { id: id, uri: uri, name: name } in this.getMappingsInfo()) {
  114.             var mapping = this._namedMappings[id];
  115.             if (mapping) {
  116.                 mapping.finalize();
  117.                 mapping.init(id, uri, name);
  118.             }
  119.         }
  120.         var winMed = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
  121.         var winEnum = winMed.getEnumerator(null);
  122.         while (winEnum.hasMoreElements()) {
  123.             var win = winEnum.getNext().QueryInterface(Ci.nsIDOMWindow);
  124.             if (win.PrefsUI) {
  125.                 win.gShouldCommit = false;
  126.                 win.close();
  127.             }
  128.         }
  129.     },
  130.  
  131.     getDBConnection: function FGS_getDBConnection(aForceOpen) {
  132.         if (!aForceOpen && !this._dbFile.exists())
  133.             return null;
  134.         if (!this._dbConn || !this._dbConn.connectionReady) {
  135.             var dbSvc = Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService);
  136.             this._dbConn = dbSvc.openDatabase(this._dbFile);
  137.         }
  138.         return this._dbConn;
  139.     },
  140.  
  141.     getLocaleString: function FGS_getLocaleString(aName) {
  142.         if (!this._stringBundle) {
  143.             var bundleSvc = Cc["@mozilla.org/intl/stringbundle;1"].
  144.                             getService(Ci.nsIStringBundleService);
  145.             this._stringBundle = bundleSvc.createBundle(BUNDLE_URI);
  146.         }
  147.         try {
  148.             return this._stringBundle.GetStringFromName(aName);
  149.         }
  150.         catch (ex) {
  151.             return aName;
  152.         }
  153.     },
  154.  
  155.     _stringBundle: null,
  156.  
  157. };
  158.  
  159.  
  160.  
  161. function NSGetModule(compMgr, fileSpec) {
  162.     return XPCOMUtils.generateModule([xdGestureService]);
  163. }
  164.  
  165.  
  166.